//
//  MSBeverageStorageController.m
//  觅尚
//
//  Created by No.8 on 2019/4/28.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSMyManagementController.h"
#import <WebKit/WebKit.h>

@interface MSMyManagementController ()<WKNavigationDelegate,UINavigationControllerDelegate,WKUIDelegate>
{
    CustomNavigationBar *naviagtionBar;
}

/** 网页*/
@property(nonatomic,strong)WKWebView *webView;

@end

@implementation MSMyManagementController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    
    [self CreatView];
    
}

- (void)CreatView{
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    
    config.preferences = [[WKPreferences alloc] init];
    
    config.preferences.minimumFontSize = 10;
    
    config.preferences.javaScriptEnabled = YES;
    
    config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
    
    config.userContentController = [[WKUserContentController alloc] init];
    
    config.processPool = [[WKProcessPool alloc] init];
    
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, ScreenHeight - [self navBarBottom]) configuration:config];
    //    webView.configuration = config;
    
    NSString * requster = [NSString stringWithFormat:@"%@busid=%@",MShome_HTML,[WYCAccountTool unarchivebelongBusid]];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:requster]]];
    webView.scrollView.bounces = NO;
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    webView.allowsBackForwardNavigationGestures = YES;
    [webView reload];
    [self.view addSubview:webView];
    self.webView = webView;
    
    
}

#pragma mark --- WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    
    [MBManager showLoading];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [MBManager hideAlert];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    [MBManager hideAlert];
}
// 在收到响应后，决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    
    WYCLog(@"webView%@",webView);
    WYCLog(@"navigationResponse%@",navigationResponse);
    
    WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
    //这句是必须加上的，不然会异常
    decisionHandler(actionPolicy);
    
}



#pragma mark -- back and close
-(void)personBack
{
    [self.webView goBack];
}

-(void)personClose
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"公司设置";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)backClick{
    
    //判断是否能返回到H5上级页面
    if (self.webView.canGoBack==YES) {
        //返回上级页面
        [self.webView goBack];
        
    }else{
        //退出控制器
        [self.navigationController popViewControllerAnimated:NO];
    }
}

@end
